{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Summary Ranges"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: summaryRanges"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #汇总区间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个 &nbsp;<strong>无重复元素</strong> 的&nbsp;<strong>有序</strong> 整数数组 <code>nums</code> 。</p>\n",
    "\n",
    "<p>返回 <em><strong>恰好覆盖数组中所有数字</strong> 的 <strong>最小有序</strong> 区间范围列表&nbsp;</em>。也就是说，<code>nums</code> 的每个元素都恰好被某个区间范围所覆盖，并且不存在属于某个范围但不属于 <code>nums</code> 的数字 <code>x</code> 。</p>\n",
    "\n",
    "<p>列表中的每个区间范围 <code>[a,b]</code> 应该按如下格式输出：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>\"a-&gt;b\"</code> ，如果 <code>a != b</code></li>\n",
    "\t<li><code>\"a\"</code> ，如果 <code>a == b</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,1,2,4,5,7]\n",
    "<strong>输出：</strong>[\"0-&gt;2\",\"4-&gt;5\",\"7\"]\n",
    "<strong>解释：</strong>区间范围是：\n",
    "[0,2] --&gt; \"0-&gt;2\"\n",
    "[4,5] --&gt; \"4-&gt;5\"\n",
    "[7,7] --&gt; \"7\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [0,2,3,4,6,8,9]\n",
    "<strong>输出：</strong>[\"0\",\"2-&gt;4\",\"6\",\"8-&gt;9\"]\n",
    "<strong>解释：</strong>区间范围是：\n",
    "[0,0] --&gt; \"0\"\n",
    "[2,4] --&gt; \"2-&gt;4\"\n",
    "[6,6] --&gt; \"6\"\n",
    "[8,9] --&gt; \"8-&gt;9\"\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>0 &lt;= nums.length &lt;= 20</code></li>\n",
    "\t<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>\n",
    "\t<li><code>nums</code> 中的所有值都 <strong>互不相同</strong></li>\n",
    "\t<li><code>nums</code> 按升序排列</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [summary-ranges](https://leetcode.cn/problems/summary-ranges/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [summary-ranges](https://leetcode.cn/problems/summary-ranges/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[0,1,2,4,5,7]', '[0,2,3,4,6,8,9]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        ranges = [] # [start, end] or [x, y]\n",
    "        for i, n in enumerate(nums):\n",
    "            if ranges and ranges[-1][1] == n-1:\n",
    "                ranges[-1][1] = n\n",
    "            else:\n",
    "                ranges.append([n, n])\n",
    "\n",
    "        return [f'{x}->{y}' if x != y else f'{x}' for x, y in ranges]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        p0, p1 = None, None\n",
    "        for i, num in enumerate(nums):\n",
    "            if i == 0:\n",
    "                p0, p1 = num, num\n",
    "            else:\n",
    "                if num == p1 + 1:\n",
    "                    p1 = num\n",
    "                else:\n",
    "                    if p0 == p1:\n",
    "                        res.append(str(p0))\n",
    "                    else:\n",
    "                        res.append(str(p0) + '->' + str(p1))\n",
    "                    p0, p1 = num, num\n",
    "        if p0 is not None and p0 == p1:\n",
    "            res.append(str(p0))\n",
    "        elif p0 is not None:\n",
    "            res.append(str(p0) + '->' + str(p1))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        if not nums:\n",
    "            return []\n",
    "\n",
    "        res = [str(nums[0])]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] - nums[i-1] > 1:\n",
    "                if res[-1] != str(nums[i-1]):\n",
    "                    res[-1] += \"->\" + str(nums[i-1])\n",
    "                res.append(str(nums[i]))\n",
    "        if str(nums[-1]) != res[-1]:\n",
    "            res[-1] += \"->\" + str(nums[-1])\n",
    "        \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums):\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        if len(nums) == 1:\n",
    "            return [str(nums[0])]\n",
    "        \n",
    "        res = []\n",
    "        final_res = []\n",
    "        left = 0\n",
    "        right = 0\n",
    "        \n",
    "        while right < len(nums)-1:\n",
    "            if nums[right+1] == nums[right]+1:\n",
    "                right += 1\n",
    "            else:\n",
    "                res.append(nums[left:right+1])\n",
    "                left = right + 1\n",
    "                right = right + 1\n",
    "            if right == len(nums)-1:\n",
    "                res.append(nums[left:]) \n",
    "                \n",
    "        for i in res:\n",
    "            if len(i) == 1:\n",
    "                final_res.append(str(i[0]))\n",
    "            else:\n",
    "                final_res.append(str(i[0])+'->'+str(i[-1]))\n",
    "                \n",
    "        return final_res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        if len(nums)==0:\n",
    "            return []\n",
    "        stack=[nums[0]]\n",
    "        ans=[]\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]-stack[-1]==1:\n",
    "                stack.append(nums[i])\n",
    "            else:\n",
    "                if len(stack)==1:\n",
    "                    ans.append(str(stack[-1]))\n",
    "                else:\n",
    "                    ans.append(str(stack[0])+'->'+str(stack[-1]))\n",
    "                stack=[nums[i]]\n",
    "        if len(stack)==1:\n",
    "            ans.append(str(stack[-1]))\n",
    "        else:\n",
    "            ans.append(str(stack[0])+'->'+str(stack[-1]))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        n = len(nums)\n",
    "        if n==0:\n",
    "            return []\n",
    "        pre_value = nums[0]\n",
    "        cur_list = [nums[0]]\n",
    "        res = []\n",
    "        def listToStr(list):\n",
    "            if len(list)>1:\n",
    "                return str(list[0])+\"->\"+str(list[-1])\n",
    "            else:\n",
    "                return str(list[0])\n",
    "        for i in range(1,n):\n",
    "            cur_value = nums[i]\n",
    "            if cur_value!=pre_value+1:\n",
    "                des = listToStr(cur_list)\n",
    "                res.append(des)\n",
    "                cur_list=[]\n",
    "            pre_value = cur_value\n",
    "            cur_list.append(cur_value)\n",
    "        res.append(listToStr(cur_list))\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        if len(nums)==0:\n",
    "            return []\n",
    "        flag = []\n",
    "        result = []\n",
    "        for i in range(len(nums)-1):\n",
    "            if nums[i+1] != nums[i]+1:\n",
    "               flag.append(i) \n",
    "        h = 0\n",
    "        for j in range(len(flag)):\n",
    "            if flag[j] - h:\n",
    "                result.append(str(nums[h])+ \"->\" + str(nums[flag[j]]))\n",
    "                h = flag[j] + 1\n",
    "            else:\n",
    "                result.append(str(nums[h]))\n",
    "                h += 1\n",
    "        if len(nums)-1 - h:\n",
    "                result.append(str(nums[h])+ \"->\" + str(nums[len(nums)-1]))\n",
    "        else:\n",
    "            result.append(str(nums[h]))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        \n",
    "        def appendStrItem(startItem,lastItem):\n",
    "            if startItem == lastItem:\n",
    "                result.append(str(lastItem))\n",
    "            else:\n",
    "                result.append(\"%d->%d\"%(startItem,lastItem))\n",
    "\n",
    "        result = []\n",
    "\n",
    "        if nums == []:\n",
    "            return []\n",
    "\n",
    "        startItem = lastItem = nums[0]\n",
    "\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i] - 1 != lastItem:\n",
    "                appendStrItem(startItem,lastItem)\n",
    "                startItem = lastItem = nums[i]\n",
    "            else:\n",
    "                lastItem = nums[i]\n",
    "        else:\n",
    "            appendStrItem(startItem,lastItem)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        if not nums:\n",
    "            return []\n",
    "\n",
    "        def append_item(i, j):\n",
    "            if i + 1 == j:\n",
    "                result.append(str(nums[i]))\n",
    "            else:\n",
    "                result.append(\"{}->{}\".format(nums[i], nums[j - 1]))\n",
    "\n",
    "        i, j = 0, 1\n",
    "        result = []\n",
    "        while j < len(nums):\n",
    "            if nums[i] + j - i != nums[j]:\n",
    "                append_item(i, j)\n",
    "                i = j\n",
    "            j += 1\n",
    "\n",
    "        append_item(i, j)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        left ,right = 0,0\n",
    "        n=len(nums)\n",
    "        ans=[]\n",
    "        res=[]\n",
    "        while left<n and right <n:\n",
    "            while left<n-1 and nums[left]+1 == nums[left+1]:\n",
    "                left=left+1\n",
    "            ans.append([nums[right],nums[left]])\n",
    "            left=right=left+1\n",
    "        for i in ans:\n",
    "            if i[0]==i[1]:\n",
    "                res.append(str(i[0]))\n",
    "            else:\n",
    "                res.append(\"{}->{}\".format(i[0],i[1]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        i = 0\n",
    "        end = len(nums) - 1\n",
    "        result = []\n",
    "        if len(nums) == 0:\n",
    "            return(nums)\n",
    "        elif len(nums) == 1:\n",
    "            return([str(nums[0])])\n",
    "        else:\n",
    "            while i < len(nums):\n",
    "                start = i\n",
    "                while i< len(nums) - 1 and nums[i] + 1 == nums[i+1]:\n",
    "                    i+=1\n",
    "                if start == i:\n",
    "                    result.append(str(nums[i]))\n",
    "                else:\n",
    "                    result.append(str(nums[start]) + '->' + str(nums[i]))\n",
    "                i+=1\n",
    "        return(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        i = 0\n",
    "        end = len(nums) - 1\n",
    "        result = []\n",
    "        if len(nums) == 0:\n",
    "            return(nums)\n",
    "        elif len(nums) == 1:\n",
    "            return([str(nums[0])])\n",
    "        else:\n",
    "            while i < len(nums):\n",
    "                start = i\n",
    "                while i< len(nums) - 1 and nums[i] + 1 == nums[i+1]:\n",
    "                    i+=1\n",
    "                if start == i:\n",
    "                    result.append(str(nums[i]))\n",
    "                else:\n",
    "                    result.append(str(nums[start]) + '->' + str(nums[i]))\n",
    "                i+=1\n",
    "        return(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        if not nums:\n",
    "            return []\n",
    "        l_str = []\n",
    "        if len(nums) == 1:\n",
    "            l_str.append('%d'%nums[0])\n",
    "            return l_str\n",
    "        l = r = nums[0]\n",
    "        for i in range(len(nums) - 1):\n",
    "            if nums[i] + 1 == nums[i + 1]:\n",
    "                r = nums[i + 1]\n",
    "                if i == len(nums) - 2:\n",
    "                    if r == l:\n",
    "                        l_str.append('%d'%l)\n",
    "                    else:\n",
    "                        l_str.append('%d'%l + '->' + '%d'%r)\n",
    "                    l = r = nums[i + 1]\n",
    "                    \n",
    "            else:\n",
    "                if r == l:\n",
    "                    l_str.append('%d'%l)\n",
    "                else:\n",
    "                    l_str.append('%d'%l + '->' + '%d'%r)\n",
    "                l = r = nums[i + 1]\n",
    "                if i + 1 == len(nums) - 1:\n",
    "                     l_str.append('%d'%l)\n",
    "        return l_str\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        res = []\n",
    "        n = 0\n",
    "        while n < len(nums):\n",
    "            if n+1 < len(nums) and nums[n+1] == nums[n]+1:\n",
    "                m = n\n",
    "                while n+1 < len(nums) and nums[n+1] == nums[n]+1:\n",
    "                    n += 1\n",
    "                res.append(f'{nums[m]}->{nums[n]}')\n",
    "            else:\n",
    "                res.append(str(nums[n]))\n",
    "            n += 1\n",
    "        return res\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        L=[]\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            if i==0:\n",
    "                s=str(nums[0])\n",
    "            if i>=1 and nums[i]!=nums[i-1]+1:\n",
    "                if s!=str(nums[i-1]):\n",
    "                    s=s+'->'+str(nums[i-1])\n",
    "                L.append(s)\n",
    "                s=str(nums[i])\n",
    "            if i==n-1:\n",
    "                if s!=str(nums[n-1]):\n",
    "                    s=s+'->'+str(nums[n-1])\n",
    "                L.append(s)\n",
    "        return L\n",
    "                \n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        qujian_list = []\n",
    "        if not nums:\n",
    "            return qujian_list\n",
    "        elif len(nums) == 1:\n",
    "            qujian_list.append(str(nums[0]))\n",
    "        else:\n",
    "            low = 0\n",
    "            high = 0\n",
    "            for i in range(1,len(nums)):\n",
    "                if nums[i] - nums[i-1] == 1:\n",
    "                    high = i\n",
    "                    if i == len(nums)-1:\n",
    "                        qujian_list.append(str(nums[low])+\"->\"+str(nums[high]))\n",
    "                    continue\n",
    "                else:\n",
    "                    if low == high:\n",
    "                        qujian_list.append(str(nums[low]))\n",
    "                    else:\n",
    "                        qujian_list.append(str(nums[low])+\"->\"+str(nums[high]))\n",
    "                    low = i\n",
    "                    high = i\n",
    "                    if i == len(nums)-1:\n",
    "                        qujian_list.append(str(nums[i]))\n",
    "                \n",
    "            return qujian_list\n",
    "                     \n",
    "        \n",
    "        return qujian_list\n",
    "                \n",
    "            \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        res_list = []\n",
    "        l,r = 0,0\n",
    "        while r < len(nums):\n",
    "            if r == len(nums)-1 or nums[r] + 1 != nums[r+1]:\n",
    "                if l == r:\n",
    "                    res_list.append(str(nums[l]))\n",
    "                    l += 1\n",
    "                    r += 1\n",
    "                else:\n",
    "                    res_list.append(str(nums[l])+\"->\"+str(nums[r]))\n",
    "                    r += 1\n",
    "                    l = r\n",
    "                    \n",
    "            elif nums[r] + 1 == nums[r+1]:\n",
    "                r += 1\n",
    "\n",
    "\n",
    "            \n",
    "        return res_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from ast import alias\n",
    "from operator import le\n",
    "from re import A\n",
    "import re\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "            alist = []\n",
    "            min_index = 0\n",
    "            if len(nums) == 1:\n",
    "                return [str(nums[0])]\n",
    "            for i in range(1, len(nums)):\n",
    "                if nums[i] - nums[i-1] == 1:\n",
    "                    if i == len(nums) -1:\n",
    "                        alist.append(\"%d->%d\" % (nums[min_index], nums[i]))\n",
    "                        break\n",
    "                    continue\n",
    "                if min_index == i-1:\n",
    "                    alist.append(str(nums[min_index]))\n",
    "                    min_index += 1\n",
    "                else:\n",
    "                    alist.append(\"%d->%d\" %(nums[min_index], nums[i-1]))\n",
    "                    min_index = i\n",
    "                if i == len(nums) - 1:\n",
    "                    alist.append(str(nums[i]))\n",
    "            return alist\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\r\n",
    "        if not nums: return []\r\n",
    "        if len(nums) == 1: return [str(nums[0])]\r\n",
    "        left, res = nums[0], []\r\n",
    "        for i in range(1, len(nums)):\r\n",
    "            if nums[i] > nums[i - 1] + 1:\r\n",
    "                if nums[i - 1] == left:\r\n",
    "                    res.append(str(left))\r\n",
    "                else:\r\n",
    "                    res.append('%s->%s' % (left, nums[i - 1]))\r\n",
    "                left = nums[i]\r\n",
    "        if left == nums[-1]:\r\n",
    "            res.append(str(left))\r\n",
    "        else:\r\n",
    "            res.append('%s->%s' % (left, nums[-1]))\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        l, r = 0, 1\n",
    "        ret = []\n",
    "        while l < len(nums):\n",
    "            while r < len(nums) and nums[r] == nums[r-1]+1:\n",
    "                r += 1\n",
    "            if l == r-1:\n",
    "                ret.append(str(nums[l]))\n",
    "            else:\n",
    "                ret.append(str(nums[l])+\"->\"+str(nums[r-1]))\n",
    "            # 注意这里在一个区间处理完毕之后，右端点r还是要+1的\n",
    "            l = r\n",
    "            r += 1\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def summaryRanges(self, nums: List[int]) -> List[str]:\n",
    "        m=len(nums)\n",
    "        if m==1:\n",
    "            ans=[1]\n",
    "            del ans[0]\n",
    "            ans.append(str(nums[0]))\n",
    "            return ans\n",
    "        else:\n",
    "\n",
    "            ans=[1]\n",
    "            del ans[0]\n",
    "            arr=0\n",
    "            for x in range(0,m-1):\n",
    "            \n",
    "                if int(nums[x+1])-int(nums[x])==1:\n",
    "                    arr+=1\n",
    "                else:\n",
    "                    if arr==0:\n",
    "\n",
    "                        ans.append(str(nums[x]))\n",
    "                    else:\n",
    "                        k=nums[x]-arr\n",
    "                    \n",
    "                        ans.append (str(k)+\"->\"+str(k+arr))\n",
    "                        arr=0\n",
    "                if x==m-2:\n",
    "                    if arr==0:\n",
    "\n",
    "                        ans.append(str(nums[x+1]))\n",
    "                    else:\n",
    "                        k=nums[x]-arr+1\n",
    "                    \n",
    "                        ans.append (str(k)+\"->\"+str(k+arr))\n",
    "        \n",
    "                    \n",
    "\n",
    "            return ans\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
